package com.zjsru.studyplan1;



/**
 * 反转字符串中的单词
 * <p>
 * 输入："Let's take LeetCode contest"
 * 输出："s'teL ekat edoCteeL tsetnoc"
 **/
public class reverseWords {
    public String reverseWords(String s) {
        String[] s1 = s.split(" ");
        String[] s2 = s1.clone();
        int n = s1.length;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < n; i++) {
            int k = s2[i].length();
            String[] ss = new String[k];
            for (int j = 0; j < k; j++) {
                ss[j] = String.valueOf(s2[i].charAt(j));
            }
            for (int j = 0; j < k; j++, k--) {
                String temp = ss[j];
                ss[j] = ss[k - 1];
                ss[k - 1] = temp;
            }
            for (int j = 0; j < ss.length; j++) {
                sb.append(ss[j]);
            }
            s2[i] = sb.toString();
            sb.delete(0, ss.length);
        }
        String resultString = "";
        int i = 0;
        while (i < s2.length) {
            resultString += s2[i] + " ";
            i++;
        }
        int length = resultString.length();
        resultString = resultString.substring(0, length - 1);
        return resultString;
    }

    public static void main(String[] args) {
        reverseWords reverseWords = new reverseWords();
        String s = new String("Let's take LeetCode contest");
        System.out.println(reverseWords.reverseWords(s));
    }

}
